<?php
/**
 * 车辆故障信息管理
 * @author hezhong
 */
class Break_down extends MY_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('car_fault_model');
    }

    public function index() {
        $param['faultid'] = $this->input->get_post('faultid');
        $param['faultname'] = $this->input->get_post('faultname');
        $param['start'] =  intval($this->input->get_post('per_page'));
        $param['length'] = 10;

        $data = $this->car_fault_model->select_car_fault_list($param);

        $this->load->library('pagination');
        $config['base_url'] = site_url('c=break_down&m=index&faultname='.$param['faultname'].'&faultid='.$param['faultid']);
        $config['total_rows'] = $data['count'];
        $config['per_page'] = 10;
        $config['cur_tag_open'] = '<span class="page_now">';
        $config['cur_tag_close'] = '</span>';

        $this->pagination->initialize($config);
        $data['pagelink'] = $this->pagination->create_links();
        
        $this->show_view("breakdownList", $data);
       
    }

    public function add() {
        $this->form_validation->set_rules('faultname', '故障名称', 'required');
        $this->form_validation->set_rules('faultinfo', '故障表现', 'required');
        $this->form_validation->set_rules('faultreason', '故障原因', 'required');
        $this->form_validation->set_rules('faultsolve', '解决办法', 'required');

        if($this->form_validation->run() === FALSE) {
            $data['types'] = $this->get_type_list();

            
            $this->show_view("breakdownAdd", $data);
           
        }else {
            $this->car_fault_model->insert();
            $this->show_message("添加车辆故障信息成功",site_url("c=break_down&m=index"));
        }
    }

    private function get_type_list() {
        $list = $this->car_fault_model->select_car_fault_type_list();

        $group_result = array();
        foreach($list as $row1) {
            if($row1['parentid'] == 0) {
                $group_result[] = $row1;
                foreach($list as $row2) {
                    if($row2['parentid'] == $row1['typeid']) {
                        $row2['typename'] = "&nbsp;&nbsp;&nbsp;-&gt;".$row2['typename'];
                        $group_result[] = $row2;
                    }
                }

            }
        }

        return $group_result;
    }

    public function edit() {
        $this->form_validation->set_rules('faultname', '故障名称', 'required');
        $this->form_validation->set_rules('faultinfo', '故障表现', 'required');
        $this->form_validation->set_rules('faultreason', '故障原因', 'required');
        $this->form_validation->set_rules('faultsolve', '解决办法', 'required');

        if($this->form_validation->run() === FALSE) {
            $data['info'] =  $this->car_fault_model->select_by_id();
            $data['types'] = $this->get_type_list();
            
            $this->show_view("breakdownEdit", $data);
           
        }else {
            $this->car_fault_model->update();
            $this->show_message("编辑车辆故障信息成功",site_url("c=break_down&m=index"));
        }
    }

    public function delete() {
        $this->car_fault_model->delete();
        $this->show_message("删除车辆故障信息成功",site_url("c=break_down&m=index"));
    }
}